<template>
  <el-container>
    <div class="c1">
      <el-form
        :inline="true"
        :model="searchData"
        :rules="rules"
        ref="searchData"
        class="demo-form-inline s1"
      >
        <el-form-item label="企业名称" prop="name">
          <el-input class="search_inp" v-model="searchData.name" placeholder="输入企业名称查询"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            type="success"
            icon="el-icon-search"
            @click="searchEnterprisesAllocated"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card class="c2">
      <el-table
        :data="tableData"
        style="width: 100%"
        max-height="400"
        align="center"
        stripe
        v-loading="loading"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column type="index" width="50" label="序号"></el-table-column>
        <el-table-column prop="enterpriseName" width="80" label="企业名称"></el-table-column>
        <el-table-column prop="enterpriseAddress" width="80" label="企业地址"></el-table-column>
        <el-table-column prop="enterpriseRegisteredCapital" width="120" label="企业注册资金"></el-table-column>
        <el-table-column prop="creditNo" width="140" label="社会统一信用代码"></el-table-column>
        <el-table-column prop="operName" width="80" label="法人姓名"></el-table-column>
        <el-table-column prop="operNameId" width="120" label="法人证件号码"></el-table-column>
        <el-table-column prop="departmentName" width="120" label="营销网点名称"></el-table-column>
        <el-table-column prop="departmentAddress" width="120" label="营销网点地址"></el-table-column>
        <el-table-column prop="sysUserName" width="80" label="营销经理"></el-table-column>
        <el-table-column width="120" label="操作" style="text-align:center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            >手动分配</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="手动分配营销经理" :visible.sync="editmenuDialogVisible" width="65%">
      <el-row style="text-align:center">
        选定营销网点：
        <el-select
          v-model="departmentId"
          placeholder="请选择营销网点"
          @change="getSysUserListBydepartmentId"
        >
          <el-option
            v-for="(item, index) in bankList"
            :label="item.name"
            :value="item.id"
            :key="index"
          ></el-option>
        </el-select>
      </el-row>
      <el-row style="text-align:center">
        选定营销经理：
        <el-select v-model="sysUserName" placeholder="请选择营销经理">
          <el-option
            v-for="(item, index) in sysUserList"
            :label="item.userName"
            :value="item.userName"
            :key="index"
          ></el-option>
        </el-select>
      </el-row>
      <el-button @click="getSysUserListBydepartmentId">手动获取管户列表</el-button>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editmenuDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allocateAndSave">确 定</el-button>
      </span>
    </el-dialog>

    <el-footer v-if="searchToggle">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.pageNum.currentPage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="this.pageNum.perPage_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.pageNum.total"
      ></el-pagination>
    </el-footer>
  </el-container>
</template>

<script>
import axios from "../../utils/axios";
export default {
  data() {
    return {
      pageNum: {
        current: 1,
        size: 10,
        total: 0
      },
      loading: false,
      searchData: {
        name: "宁波市"
      },
      rules: {
        name: [
          { required: true, message: "请输入企业名称", trigger: "blur" },
          { min: 5, message: "长度在 3 个字符以上", trigger: "blur" }
        ]
      },
      tableData: [],
      searchToggle: true,
      editmenuDialogVisible: false,
      enterpriseId: "",
      departmentId: "",
      sysUserName: "zgc",
      bankList: [],
      sysUserList: []
    };
  },
  methods: {
    //1.查询已被分配营销经理的企业列表
    searchEnterprisesAllocated() {
      this.getEnterpriseList();
    },
    //企业列表列表
    getEnterpriseList() {
      if (
        this.searchData.name == "" ||
        this.searchData.name == null ||
        this.searchData.name.length < 3
      ) {
        return;
      }
      axios({
        method: "POST",
        url: "/sys/tEnterprise/getList",
        data: { name: this.searchData.name }
      }).then(res => {
        this.tableData = res;
        this.loading = false;
        this.pageNum.total = res.length;
      });
    },
    //2,点击分配按钮,获取银行网点
    handleEdit(row) {
      this.editmenuDialogVisible = true;
      this.getBankList();
      this.enterpriseId = row.enterpriseId;
    },
    getBankList() {
      axios({
        method: "GET",
        url: "sys/department/getBankList"
      }).then(res => {
        this.bankList = res;
      });
    },
    //3.选择网点后，回显对应网点下的系统管户sys_user
    getSysUserListBydepartmentId() {
      axios({
        method: "POST",
        url: "/sysUser/getList",
        data: { bankId: this.departmentId }
      }).then(res => {
        this.sysUserList = res;
      });
    },
    //4.分配并保存企业和网点之间的管户关系
    allocateAndSave() {
      axios({
        method: "POST",
        url: "sys/tEnterprise/allocateAndSave",
        data: {
          enterpriseId: this.enterpriseId,
          departmentId: this.departmentId,
          sysUserName: this.sysUserName
        }
      }).then(res => {
        console.log(res);
        this.editmenuDialogVisible = false;
        this.getEnterpriseList();
      });
    },
    handleSizeChange() {},
    handleCurrentChange() {}
  }
};
</script>
<style lang="less" scoped>
.c1 {
  display: flex;
  justify-content: start;
  margin-bottom: 1.5rem;
  width: 100%;
  .dBtn {
    margin-right: 1rem;
  }
}

.el-input /deep/.el-input__inner {
  height: 3.05rem;
  font-size: 12px !important;
}
</style>